﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using ServiceBase;
using System.Diagnostics;

namespace ServiceMatrix
{
    public static class Builder_Matrix
    {
        public enum MatrixType
        {
            零一字符串,
            零一逗号分隔字符串,
            零一字符串矩阵,
            零一逗号分隔字符串矩阵
        }
        /// <summary>
        /// 将字模转1、0字符串
        /// </summary>
        /// <param name="bmp"></param>
        /// <param name="separator">分隔符</param>
        /// <param name="isEnter">是否换行</param>
        /// <returns></returns>
        public static string Bitmap2Str(Bitmap bmp, string separator,bool isEnter)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bmp.Height; i++)
            {
                for (int j = 0; j < bmp.Width; j++)
                {
                    if (BaseFunction.IsEmptyPixel(bmp, j, i))
                    {
                        sb.Append("0");
                    }
                    else
                    {
                        sb.Append("1");
                    }
                    sb.Append(separator);

                }
                if (isEnter)
                {
                    sb.Remove(sb.Length - separator.Length, separator.Length);
                    sb.Append(Environment.NewLine);
                }
            }
            if (!isEnter)
            {
                sb.Remove(sb.Length - separator.Length, separator.Length);
            }
            return sb.ToString();
        }

        /// <summary>
        /// 按照指定的类型生成字模，并输出到指定的文件。
        /// </summary>
        /// <param name="list"></param>
        /// <param name="path"></param>
        /// <param name="type"></param>
        public static void Create(Bitmap[] list, string path, MatrixType type)
        {
            StringBuilder sb = new StringBuilder();
            foreach (Bitmap bmp in list)
            {
                switch (type)
                {
                    case MatrixType.零一字符串:
                        sb.Append(Bitmap2Str(bmp, "", false));
                        break;
                    case MatrixType.零一逗号分隔字符串:
                        sb.Append(Bitmap2Str(bmp, ",", false));
                        break;
                    case MatrixType.零一字符串矩阵:
                        sb.Append(Bitmap2Str(bmp, "", true));
                        break;
                    case MatrixType.零一逗号分隔字符串矩阵:
                        sb.Append(Bitmap2Str(bmp, ",", true));
                        break;
                    default:
                        break;
                }
                sb.Append(Environment.NewLine);
            }
            Common.FileHelper.WriteFile(path, sb.ToString());
            Process.Start(path);
        }
    }
}
